<template>
    <el-dialog v-model="dialogFormVisible" :title="title" @close="closeDialog" :z-index="1000">
        <el-form :model="addBaseDepartmentForm" ref="addBaseDepartmentRef" :rules="addBaseDepartmentrules">
                        <el-form-item :label="type==1?'类别名称':type==2?'项目名称':'评价点'" :label-width="formLabelWidth"
                                      prop="name">
                            <el-input
                                    v-model="addBaseDepartmentForm.name"
                                    autocomplete="off"
                                    ref="name"
                                    :placeholder="type==1?'请填写类别名称':type==2?'请填写项目名称':'请填写评价点名称'"
                            ></el-input>
                        </el-form-item>
                        <!-- <el-form-item label="父部门id" :label-width="formLabelWidth"
                                      prop="parentId" v-if="addBaseDepartmentForm.id">
                            <el-input
                                    v-model="addBaseDepartmentForm.parentId"
                                    autocomplete="off"
                                    ref="name"
                                    placeholder="请填写父部门id"
                            ></el-input>
                        </el-form-item> -->
                        <el-form-item label="权重" :label-width="formLabelWidth"
                                      prop="sortNum">
                            <el-input
                                    v-model="addBaseDepartmentForm.sortNum"
                                    autocomplete="off" type="number"
                                    ref="name"
                                    placeholder="请填写权重"
                            ></el-input>
                        </el-form-item>
                        <!-- <el-form-item label="部门类型" :label-width="formLabelWidth"
                                      prop="type" v-if="addBaseDepartmentForm.id">
                            <el-input
                                    v-model="addBaseDepartmentForm.type"
                                    autocomplete="off"
                                    ref="name"
                                    placeholder="请填写部门类型"
                            ></el-input>
                        </el-form-item>
                        <el-form-item label="是否可用" :label-width="formLabelWidth"
                                      prop="isEffect" v-if="addBaseDepartmentForm.id">
                            <el-input
                                    v-model="addBaseDepartmentForm.isEffect"
                                    autocomplete="off"
                                    ref="name"
                                    placeholder="请填写是否可用"
                            ></el-input>
                        </el-form-item>-->
                         <el-form-item v-if="type==3" label="配分" :label-width="formLabelWidth"
                                      prop="campus" >
                            <el-input
                                    v-model="addBaseDepartmentForm.campus"
                                    autocomplete="off"
                                    ref="name"
                                    type="number"
                                    placeholder="请填写配分"
                            ></el-input>
                        </el-form-item> 
                        <el-form-item v-if="type==3" label="评价说明" :label-width="formLabelWidth"
                                      prop="campusName">
                            <el-input
                                    v-model="addBaseDepartmentForm.campusName"
                                    autocomplete="off"
                                    ref="name"
                                    type="textarea" rows="6"

                                    placeholder="请填写评价说明"
                            ></el-input>
                        </el-form-item> 
                        <el-form-item v-if="type==3" label="判分原则" :label-width="formLabelWidth"
                                      prop="detail">


                            <el-input
                                    v-model="addBaseDepartmentForm.detail"
                                    autocomplete="off"
                                    ref="name"
                                    type="textarea" rows="6"
                                    placeholder="请填写判分原则"
                            ></el-input>
                        </el-form-item> 
        </el-form>
        <template #footer>
      <span class="dialog-footer">
        <el-button @click="cancel()" plain >取消</el-button>
        <el-button type="primary" @click="save()" plain>保存</el-button>
      </span>
        </template>
    </el-dialog>
</template>

<script>
    import {ref} from "vue";
    import {httpService} from "@/utils/httpService";
    import {ElMessage, ElForm} from "element-plus";
    import {vxRule} from "@/utils/validator";

    export default {
        name: "AddBaseDepartment",
        components: {ElForm, ElMessage},
        data() {
            return {
                dialogFormVisible: ref(false),
                formLabelWidth: "120px",
                title: "",
                type:0,
                addBaseDepartmentForm: {
                    name: "",
                    parentId: "",
                    sortNum: "",
                    type: "",
                    isEffect: "",
                    campus: "",
                    campusName: "",
                    detail:""
                },
                // 校验规则
                addBaseDepartmentrules: {
                    name: vxRule(true, null, "blur"),
                    sortNum: vxRule(true, null, "blur"),
                },
            };
        },
        methods: {
            closeDialog() {
                this.clear();
            },
            cancel() {
                this.dialogFormVisible = false;
                this.clear();
            },
            getBaseDepartmentById(id) {
                httpService("/javaApi/baseQuestionBank/queryById", {id: id}, "get").then(
                    (data) => {
                        this.addBaseDepartmentForm = data;
                    }
                );
            },
            clear() {
                this.dialogFormVisible = false;
                //清空form
                    this.addBaseDepartmentForm.id = "";
                    this.addBaseDepartmentForm.name = "";
                    this.addBaseDepartmentForm.parentId = "";
                    this.addBaseDepartmentForm.sortNum = "";
                    this.addBaseDepartmentForm.type = "";
                    this.addBaseDepartmentForm.isEffect = "";
                    this.addBaseDepartmentForm.campus = "";
                    this.addBaseDepartmentForm.campusName = "";
                    this.addBaseDepartmentForm.detail = "";
            },
            save() {
                this.$refs.addBaseDepartmentRef.validate((valid) => {
                    if (valid) {
                        let params = new FormData(); //创建一个form对象,必须是form对象否则后端接受不到数据
                        params.append("name", this.addBaseDepartmentForm.name);
                        params.append("parentId", this.addBaseDepartmentForm.parentId);
                        params.append("sortNum", this.addBaseDepartmentForm.sortNum);
                        // params.append("type", this.addBaseDepartmentForm.type);
                        // params.append("isEffect", this.addBaseDepartmentForm.isEffect);
                        if(this.type==3){
                            params.append("campus", this.addBaseDepartmentForm.campus);
                        params.append("campusName", this.addBaseDepartmentForm.campusName);
                        params.append("detail", this.addBaseDepartmentForm.detail);
                        }
                        if (this.addBaseDepartmentForm.id) {
                            params.append("id", this.addBaseDepartmentForm.id);
                            httpService("/javaApi/baseQuestionBank/edit", params, "post").then(
                                (data) => {
                                    if (data.code == 200) {
                                        ElMessage.success("修改成功");
                                        this.clear();
                                        //刷新父组件的数据
                                        this.$parent.getBaseDepartmentList();
                                    } else {
                                        ElMessage.error("修改失败");
                                    }
                                }
                            );
                        } else {
                            httpService("/javaApi/baseQuestionBank/add", params, "post").then(
                                (data) => {
                                    if (data.code == 200) {
                                        ElMessage.success("添加成功");
                                        this.clear();
                                        //刷新父组件的数据
                                        this.$parent.getBaseDepartmentList();
                                    } else {
                                        ElMessage.error("添加失败");
                                    }
                                }
                            );
                        }
                    } else {
                        return false;
                    }
                });
            },
        },
    };
</script>
<style>
</style>